package com.ruoyi.time;

import com.ruoyi.system.domain.AhhcDailySummary;
import com.ruoyi.system.domain.AhhcIntakeOutputRecords;
import com.ruoyi.system.mapper.AhhcDailySummaryMapper;
import com.ruoyi.system.mapper.AhhcIntakeOutputRecordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.sql.Date;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Component
public class timeSerice {
    @Autowired
    AhhcIntakeOutputRecordsMapper ahhcIntakeOutputRecordsMapper;
    @Autowired
    AhhcDailySummaryMapper ahhcDailySummaryMapper;

    /// 扫描患者记录表-记录所有患者的当天数据保存
    public void cronTask(int classify) {
        //获取一下当前日期--后期日期修复也用到这个 --
        LocalDate yesterday;
        if (classify == 1) {
            //打印日志
            System.out.println("定时任务执行------每日更新临床数据");
            yesterday = LocalDate.now().minusDays(1);
        } else {
            yesterday = null;

        }

        //查询前一天所有记录
        //    select * from ahhc_intake_output_records where record_date = #{yesterday}
        List<AhhcIntakeOutputRecords> ahhcIntakeOutputRecords =
                ahhcIntakeOutputRecordsMapper.selecerday(yesterday);
        //流操作。先分类用户，计算总数据，存到save
        if(ahhcIntakeOutputRecords.isEmpty())
        {
            return;
        }
        System.out.println("定时任务执行");
        /**
         * Map<String, List<Record>> groupedRecords = records.stream()
         *             .collect(Collectors.groupingBy(Record::getPatientId));
         */
        Map<String, List<AhhcIntakeOutputRecords>> groupedRecords =
                ahhcIntakeOutputRecords.stream()
                        .collect
                                (Collectors.groupingBy(AhhcIntakeOutputRecords::getPatientId));

        //解析--- 根据患者ID分组完毕

        AtomicReference<Long> RLtotal_intake = new AtomicReference<>(0L);
        AtomicReference<Long> CLtotal_intake = new AtomicReference<>(0L);
        //patientId ->对应患者当日记录表
        groupedRecords.forEach((patientId, records) -> {
                    records.forEach(item -> {
                        // 获取当前记录的摄入总量（waterTotal）
                        BigDecimal waterTotal = item.getWaterTotal();
                        if (waterTotal.longValue() > 0L) {
                            /// 入量
                            RLtotal_intake.set(RLtotal_intake.get() + waterTotal.longValue());
                        } else {
                            /// 出量  改为正数
                            waterTotal = waterTotal.abs();
                            CLtotal_intake.set(CLtotal_intake.get() + waterTotal.longValue());
                            //出量加1000
                            waterTotal = waterTotal.add(new BigDecimal(1000));
                        }

                    });
                    //存入数据库-检测一下当天是更新还是什么
                   AhhcDailySummary iftj=new AhhcDailySummary();
                   iftj.setSummaryDate(Date.valueOf(yesterday));
                   iftj.setPatientId(patientId);
                   AhhcDailySummary ahhcDailySummary =ahhcDailySummaryMapper.selectlisttime(iftj);

                   if(ahhcDailySummary==null)
                   {
                       ahhcDailySummary  = new AhhcDailySummary();
                       ahhcDailySummary.setPatientId(patientId);
                       ahhcDailySummary.setTotalIntake(new BigDecimal(RLtotal_intake.get()));
                       ahhcDailySummary.setTotalOutput(new BigDecimal(CLtotal_intake.get()));
                       ahhcDailySummary.setSummaryDate(Date.valueOf(yesterday));
                       ahhcDailySummary.setCreatedTime(new Date(System.currentTimeMillis()));
                       ahhcDailySummaryMapper.insertAhhcDailySummary(ahhcDailySummary);
                   }else
                   {
                       ahhcDailySummary.setPatientId(patientId);
                       ahhcDailySummary.setTotalIntake(new BigDecimal(RLtotal_intake.get()));
                       ahhcDailySummary.setTotalOutput(new BigDecimal(CLtotal_intake.get()));
                       ahhcDailySummary.setSummaryDate(Date.valueOf(yesterday));

                       ahhcDailySummaryMapper.updateAhhcDailySummary(ahhcDailySummary);
                   }

                }
        );


    };

    }


